﻿@inject IMainPlanService MainPlanService

<div align="center">
    <label>@_title</label>
</div>
<Chart @ref="_barChart" ChartType="ChartType.Bar" OnInitAsync="@OnInit" Height=300px />
@code {
    private Chart _barChart = default!;
    private List<ChartDoubleDataDto> prodChartDataDtos = new();
    private List<ChartDoubleDataDto> ftyChartDataDtos = new();
    private List<ChartDoubleDataDto> ordChartDataDtos = new();
    private string _title = string.Empty;
    private int _year;
    protected override async Task OnInitializedAsync()
    {
        _year = DateTime.Today.Year;
        var ytdProdCycle = await MainPlanService.GetBlazorProductionCycleTimeYtdAsync(_year);
        var ytdFtyCycle = await MainPlanService.GetBlazorFactoryCycleTimeYtdAsync(_year);
        var ytdOrdCycle = await MainPlanService.GetBlazorOrderCycleTimeYtdAsync(_year);

        _title = $"{_year}年周期统计，YTD生产{ytdProdCycle} /工厂{ytdFtyCycle} /订单{ytdOrdCycle}(Days)";
    }

    private async Task<ChartDataSource> OnInit()
    {
        prodChartDataDtos = await MainPlanService.GetBlazorProductionCycleTimeAsync(_year);
        
        ftyChartDataDtos = await MainPlanService.GetBlazorFactoryCycleTimeAsync(_year);
        
        ordChartDataDtos = await MainPlanService.GetBlazorOrderCycleTimeAsync(_year);
        

        var ds = new ChartDataSource();
        ds.Options.X.Title = "Month";
        ds.Options.Y.Title = "Production CycleTime(Days)";
        ds.Options.X.Stacked = false;
        ds.Options.Y.Stacked = false;
        ds.Labels = prodChartDataDtos.Select(x => $"{x.Label}月");
        ds.Data.Add(new ChartDataset
        {
                Label = "生产周期",
                Data = prodChartDataDtos.Select(x => x.Value).Cast<object>(),
        });
        ds.Data.Add(new ChartDataset
        {
                Label = "工厂周期",
            Data = ftyChartDataDtos.Select(x => x.Value).Cast<object>(),
        });
        ds.Data.Add(new ChartDataset
        {
                Label = "订单周期",
            Data = ordChartDataDtos.Select(x => x.Value).Cast<object>(),
        });
        return ds;
    }

}
